package com.ty.user.starter.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ty.user.starter.entity.User;
import com.ty.user.starter.util.ShiroUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 统一设置自动填充
 *
 * @author minku
 * @date 2021-10-21
 */
@Component
public class MgtMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class);
        User user = ShiroUtils.getUser();
        if (null != user) {
            this.setFieldValByName("createUserId", user.getId(),metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
        User user = ShiroUtils.getUser();
        if (null != user) {
            this.setFieldValByName("updateUserId", user.getId(),metaObject);
        }
    }
}
